package com.dizang.sharding.common.mapper;

import java.util.Iterator;
import java.util.Set;

import org.apache.ibatis.mapping.MappedStatement;

import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

/**
 * 
 * @author kelvin
 * @date 2019-10-01
 */
public class UpdateCaseWhenProvider extends MapperTemplate {

	public UpdateCaseWhenProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
		super(mapperClass, mapperHelper);
	}

	/**
	 * 批量插入
	 *
	 * @param ms
	 */
	public String updateCaseWhen(MappedStatement ms) {
		final Class<?> entityClass = getEntityClass(ms);
		// 开始拼sql
		StringBuilder sql = new StringBuilder();
		Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
		Set<EntityColumn> pkColumns = EntityHelper.getPKColumns(entityClass);
		// 选择表
		sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "list[0]"));
		sql.append("<trim prefix=\"set\" suffixOverrides=\",\">");
		for (EntityColumn column : columnList) {
			if (column.isId()) {
				continue;
			}
			sql.append("<trim prefix= ");
			// 更新的字段
			sql.append("\"" + column.getColumn() + "= case\" ");
			sql.append("suffix=\"end,\">");
			sql.append("<foreach collection=\"list\" item=\"item\" index=\"index\">");
			// 条件
			sql.append(" when ");
			String buildMultiPkCaseCondition = buildMultiPkCaseCondition(pkColumns);
			sql.append(buildMultiPkCaseCondition);
			// 值
			sql.append(" then ");
			sql.append(column.getColumnHolder("item"));
			sql.append("</foreach>");
			sql.append("</trim>");
		}
		sql.append("</trim>");

		// 根据主键更新
		sql.append("<where>");
		String buildMultiPkCondition = buildMultiPkWhereCondition(pkColumns);
		sql.append(buildMultiPkCondition);
		sql.append("</where>");

		return sql.toString();
	}

	String buildMultiPkCaseCondition(Set<EntityColumn> pkColumns) {
		StringBuilder sql = new StringBuilder();
		// 为了兼容多个主键
		sql.append("(");
		for (Iterator<EntityColumn> iterator = pkColumns.iterator(); iterator.hasNext();) {
			EntityColumn entityColumn = (EntityColumn) iterator.next();
			sql.append(entityColumn.getColumn());
			if (iterator.hasNext()) {
				sql.append(",");
			}
		}
		sql.append(")");

		sql.append(" = ");


		// 每一组主键的值
		sql.append("(");
		for (Iterator<EntityColumn> iterator = pkColumns.iterator(); iterator.hasNext();) {
			EntityColumn entityColumn = (EntityColumn) iterator.next();
			sql.append(entityColumn.getColumnHolder("item"));
			if (iterator.hasNext()) {
				sql.append(",");
			}
		}
		sql.append(")");

		return sql.toString();
	}

	String buildMultiPkWhereCondition(Set<EntityColumn> pkColumns) {
		StringBuilder sql = new StringBuilder();
		// 为了兼容多个主键
		sql.append("(");
		for (Iterator<EntityColumn> iterator = pkColumns.iterator(); iterator.hasNext();) {
			EntityColumn entityColumn = (EntityColumn) iterator.next();
			sql.append(entityColumn.getColumn());
			if (iterator.hasNext()) {
				sql.append(",");
			}
		}
		sql.append(")");

		sql.append(" in ");

		sql.append("<foreach collection=\"list\" index=\"index\" item=\"item\" separator=\",\" open=\"(\" close=\")\">");

		// 每一组主键的值
		sql.append("(");
		for (Iterator<EntityColumn> iterator = pkColumns.iterator(); iterator.hasNext();) {
			EntityColumn entityColumn = (EntityColumn) iterator.next();
			sql.append(entityColumn.getColumnHolder("item"));
			if (iterator.hasNext()) {
				sql.append(",");
			}
		}
		sql.append(")");

		sql.append("</foreach>");
		return sql.toString();
	}

}
